<?php session_start();?>
<!DOCTYPE HTML/>
<html>
	<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
		<?php include "include.php"?>
	</head>
	<body onload="set_menu('qna');">
		<?php include "header.php";
			include "connect.php";
		
			$postPerPage=5;
			$page=$_GET['page'];
			$firstPost=$_GET['first'];
			
			$query="SELECT * FROM post ORDER BY no ASC";
			$result=mysql_query($query);
			$totalPost=mysql_num_rows($result);
			
			$totalPage=ceil($totalPost/$postPerPage);
			
			if($page=='') $page=1;
			else if($page<1 || $page>$totalPage) 
				echo "
					<script>
						alert('Invalid Access.');
						history.go(-1);
					</script>
				";
			
			if($firstPost=='') $firstPost=$totalPost-(($page-1)*$postPerPage);
		?>
		
		<div class="container">
			<div class="text-center">
				<h1>My Question &amp; Answer</h1>
			</div>
			
			<?php 
				for($i=$firstPost-1;$i>=0 && $i>($firstPost-$postPerPage-1);$i--){
					mysql_data_seek($result, $i);
					$row=mysql_fetch_array($result);
					$time=strtok($row[time], " ");
					$comment=mysql_num_rows(mysql_query("SELECT * FROM comment WHERE no=$row[no]"));
					
					echo '
						<div>
							<span class="crimsonText">
								<span class="muted">
									<small>'.$row[no].'</small>
								</span>
								<span style="font-size:23px;">
									<i class="icon-asterisk"></i>
									<a href="view.php?no='.$row[no].'">
										<small> '.$row[title].' </small>
									</a>
								</span>
							</span>
						    <span class="crimsonLink pull-right">
						    	<a href="#"> '.$row[writer].'</a> 
						    	| '.$time.'
								| <i class="icon-comment"></i> '.$comment.'
								| <i class="icon-heart"></i> '.$row[hit].'
						    </span>
							<hr/>
						</div>						
					';
				}
			?>
			
			<div class="pagination pagination-centered">
				<ul>
					<?php 
						$print='';
							
						for($i=1;$i<=$totalPage;$i++){							
							if($page==$i) $print=$print."<li><span class='crimsonText'><a href='qna.php?page=$i'>$i</a></span></li>";
							else $print=$print."<li><span class='crimsonLink'><a href='qna.php?page=$i'>$i</a></span></li>";
						}

						echo $print;
					?>		
				</ul>
			</div>
			        
      </div>		
	<script src="js/menu.js"></script>
	</body>
</html>